package configuration

import (
	"administrator/app/mapper"
	"administrator/app/model"
	"gitee.com/aurora-engine/utils/pageutils"
	"gitee.com/aurora-engine/utils/rep"
	"gitee.com/aurora-engine/web"
	"time"
)

func NewAccountManagement() web.Constructor {
	return func() any {
		return &AccountManagement{}
	}
}

type AccountManagement struct {
	*mapper.AccountManageMapper
}

func (manage *AccountManagement) Add(args *model.UserModel) (rep.Response, error) {
	var err error
	now := time.Now().Format(time.DateTime)
	args.CreatTime = now
	args.UpdateTime = now
	args.Birth = now
	args.Status = 1
	if err = manage.AddAccount(args); err != nil {
		return rep.Fail(err, "添加失败"), err
	}
	return rep.Success(true, "添加成功"), nil
}

// AllAccount
// @Description :
// @Param args :
// [Get] => /api/configuration/user/accounts
func (manage *AccountManagement) AllAccount(args map[string]int) (rep.Response, error) {
	var accounts []*model.Account
	var err error
	var count int64
	limit, offset := 0, 0
	if limit, offset, err = pageutils.PageNumber(args["page"], args["size"]); err != nil {
		return rep.Fail(err, "页码错误查询失败"), err
	}
	args["star"] = offset
	args["end"] = limit
	if accounts, count, err = manage.Accounts(args); err != nil {
		return rep.Fail(err, "获取账号失败"), err
	}
	page := rep.NewPage(count, accounts)
	return rep.Success(page, "查询成功"), nil
}

// Del
// @Description :
// @Param args :
// [Post] => /api/configuration/user/del
func (manage *AccountManagement) Del(args *model.UserModel) (rep.Response, error) {
	var err error
	if err = manage.DeleteUser(args); err != nil {
		return rep.Fail(err, "账号删除失败"), err
	}
	return rep.Success(true, "成功删除"), nil
}

func (manage *AccountManagement) GetUserInfo(args *model.UserModel) (rep.Response, error) {

	return rep.Success(true), nil
}

// GetOrgList
// @Description :
// @Param args :
// [Get] => /api/configuration/user/orgList
func (manage *AccountManagement) GetOrgList(args *model.UserModel) (rep.Response, error) {
	var err error
	var list []int
	if list, err = manage.SelectUserORG(args); err != nil {
		return rep.Fail(err, "查询用户组织失败"), err
	}
	return rep.Success(list), nil
}
